export enum KEYS {
    /** 站点标识 */
    SITE_INFO = 'site_info',

    /** 登录后的用户接口内容 */
    LOGIN_INFO = 'loginInfo',

    /** 记住密码后，保存登录页输入框的内容(账号、密码) */
    LOGIN_INPUT_CONTENT = 'loginInputContent',

    /** 质量评估答案缓存 */
    QUALITY_EVALUATE = 'qualityEvaluate',

    /** 登录短信发送时间 */
    SMS_TIME = 'sms_time',
}

/**
 * 获取 localStorage
 * @param key
 * @returns
 */
export const getLocalStorage = (key: keyof typeof KEYS) => {
    const res = localStorage.getItem(KEYS[key]) as any

    try {
        return JSON.parse(res)
    } catch (e) {
        return res
    }
}

/**
 * 是否存有 localStorage
 * @param key
 * @returns
 */
export const hasLocalStorage = (key: keyof typeof KEYS) => {
    return getLocalStorage(key) !== null
}

/**
 * 通用写入 localStorage
 * @param key
 * @param val
 */
export const setLocalStorage = (key: keyof typeof KEYS, val: any) => {
    localStorage.setItem(KEYS[key], typeof val === 'object' ? JSON.stringify(val) : val)
}

/**
 * 删除
 * @param key
 */
export const delLocalStorage = (key: keyof typeof KEYS) => {
    localStorage.removeItem(KEYS[key])
}
